#include "functionbuttonspanel.h"

FunctionButtonsPanel::FunctionButtonsPanel( QWidget* parent ) : QWidget( parent )
{
    this->initView();
}

void FunctionButtonsPanel::initView()
{
    QString style = "QToolButton{"
                    "border:2px solid #b5cab4;"
                    "color:#ffffff;"
                    "font-size:18px;"
                    "background:#292d33;"
                    "padding-top:18px;"
                    "border-radius:6px;"
                    "}"
                    "QToolButton:hover{"
                    "border-color:#2fb4fe;"
                    "}"
                    "QToolButton:checked{"
                    "border-color:#2fb4fe;"
                    "}"
                    "QToolButton:pressed{"
                    "border-color:#2fb4fe;"
                    "}";
    QStringList titles;
    titles << "功能1"
           << "功能2"
           << "功能3"
           << "功能4";

    QHBoxLayout* btnLay = new QHBoxLayout;
    this->m_GroupBox    = new QGroupBox( this );
    this->m_GroupBox->setStyleSheet( QString( "QGroupBox{border:0px solid #79716b;border-radius:6px;background:#433e3a;}" ) );
    for ( int i = 0; i < titles.length(); i++ )
    {
        QToolButton* btn = new QToolButton( this );
        btn->setProperty( "nav_index", i );
        btn->setIconSize( QSize( 48, 48 ) );
        btn->setIcon( QIcon( ":resource/func.png" ) );
        btn->setText( titles.at( i ) );
        btn->setToolButtonStyle( Qt::ToolButtonTextUnderIcon );
        btn->setMinimumWidth( 96 );
        btn->setMinimumHeight( 120 );
        btn->setCheckable( true );
        btn->setStyleSheet( style );

        btnLay->addWidget( btn );

        connect( btn, SIGNAL( clicked( bool ) ), this, SLOT( onNabitemClicked( bool ) ) );

        this->m_Buttons.append( btn );
    }

    this->m_Buttons.at( 0 )->setChecked( true );

    this->m_GroupBox->setLayout( btnLay );
    QVBoxLayout* layout = new QVBoxLayout;
    layout->setContentsMargins( 0, 0, 0, 0 );
    layout->addWidget( this->m_GroupBox );
    this->setLayout( layout );
    this->setFixedSize( 600, 150 );
}

void FunctionButtonsPanel::onNabitemClicked( bool clicked )
{
    Q_UNUSED( clicked )
    QToolButton* btn = dynamic_cast< QToolButton* >( sender() );

    if ( btn != Q_NULLPTR )
    {
        foreach ( QToolButton* b, this->m_Buttons )
        {
            b->setChecked( false );
        }

        btn->setChecked( true );
    }
}
